const express = require("express")
const app = express()
const path = require("node:path")
const fs = require("node:fs/promises")

app.set("view engine","ejs")
app.set("views",path.resolve(__dirname,"views"))
app.use(express.static(path.resolve(__dirname,"./public")))
app.use(express.urlencoded())

app.use("/students",require("./routes/student"))




// app.get("/delstu",(req,res) => {
//     const id = +req.query.id
//     console.log(id);
//     STUARR = STUARR.filter((stu) => stu.id !==id)
//     fs.writeFile(
//         path.resolve(__dirname,"./data/students.json"),
//         JSON.stringify(STUARR)
//     ).then(() => {
//         res.redirect("/students")
//     }).catch(() => {        
//     })   
// })


// app.get("/revise",(req,res) => {
//     const id = +req.query.id
//     const student = STUARR.find(item => item.id === id)
//     res.render("revise",{student})
// })

// app.post("/comrevise",(req,res) => {
   
//     const id = req.query.id
//     const{name,age,gender,address} = req.body
//     const student = STUARR.find(item => item.id == id)
//     student.name = name
//     student.age = +age
//     student.gender = gender
//     student.address = address

//     fs.writeFile(
//         path.resolve(__dirname,"./data/students.json"),
//         JSON.stringify(STUARR)
//     ).then(() => {
//         res.redirect("/students")
//     }).catch(() => {
            
//     })
// })

app.listen(3000,() => {

    console.log("服务器已连接");
})